package com.geek.concurrence4work;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.FutureTask;

/**
 * @Author: lishuangqiang
 * @Date: 2021/11/28
 * @Description:
 */

public class CountDownLatchMethod {

    static class MyThread implements Callable {

        private CountDownLatch countDownLatch;

        MyThread(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public Object call() throws Exception {
            countDownLatch.countDown();
            return Homework03.sum2(36);
        }


    }


    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        MyThread myThread = new MyThread(countDownLatch);
        FutureTask futureTask = new FutureTask(myThread);
        long start = System.currentTimeMillis();
        futureTask.run();
        countDownLatch.await();
        Object result = futureTask.get();
        // 确保  拿到result 并输出
        System.out.println("异步计算结果为：" + result);
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

    }
}
